package logic

import (
	"context"
	"net/http"
	"path/filepath"
	"short-video-manager/pkg"

	"short-video-manager/api/internal/svc"
	"short-video-manager/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UploadLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUploadLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UploadLogic {
	return &UploadLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UploadLogic) Upload(req *http.Request) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	file, m, err := req.FormFile("file")
	if err != nil {
		return &types.Response{
			Message: err.Error(),
			Data:    nil,
			Code:    500,
		}, nil
	}
	//videoType := 0
	ext := filepath.Ext(m.Filename)
	//switch videoType {
	//case 1:
	//
	//	if ext != ".jpg" && ext != ".png" && ext != ".webp" {
	//		return &types.Response{
	//			Message: "图片仅支持JPG/PNG/WEBP格式",
	//			Data:    nil,
	//			Code:    500,
	//		}, nil
	//	}
	//	if m.Size > 5*1024*1024 {
	//		return &types.Response{
	//			Message: "图片大小小于5M",
	//			Data:    nil,
	//			Code:    500,
	//		}, nil
	//	}
	//case 2:
	if ext != ".mp4" && ext != ".avi" {
		return &types.Response{
			Message: "视频仅支持MP4/AVI格式",
			Data:    nil,
			Code:    500,
		}, nil
	}
	if m.Size >= 200*1024*1024 {
		return &types.Response{
			Message: "视频大小小于200M",
			Data:    nil,
			Code:    500,
		}, nil
	}
	//}

	upload, err := pkg.Upload(file, m)
	if err != nil {
		return &types.Response{
			Message: err.Error(),
			Data:    nil,
			Code:    500,
		}, nil
	}
	return &types.Response{
		Message: "文件上传成功",
		Data:    upload,
		Code:    200,
	}, nil
}
